<?php
	/**
	* Products Get list 
	* return in Json format
	* @author trungdt
	* @since Oct 5, 2011
	*/
	if (!defined('EXPONENT')) exit('');

	$have_permission=false;
	if (exponent_permissions_check(array("adminisatrate", "configure", "edit", "create", "delete"),$loc))
	{
		$sql_where="";
		// build sql where
		if (isset($_REQUEST['sku']))
		{
			$sku=_ab_safe_html_string($_REQUEST['sku']);
			$sql_where.=" AND (sku LIKE '{$sku}')";
		}
		
		if (isset($_REQUEST['title']))
		{
			$title=_ab_safe_html_string($_REQUEST['title']);
			$sql_where.=" AND (title LIKE '{$title}')";
		}
		
		if (isset($_REQUEST['cat_id']))
		{
			$cat_id=intval($_REQUEST['cat_id']);
			$sql_where.=" AND (cat_id ={$cat_id})";
		}
		
		if (isset($_REQUEST['brand_id']))
		{
			$brand_id=intval($_REQUEST['brand_id']);
			$sql_where.=" AND (brand_id ={$brand_id})";
		}
		
		if (isset($_REQUEST['product_state']))
		{
			$product_state=intval($_REQUEST['product_state']);
			$sql_where.=" AND (product_state = {$product_state})";
		}
		
		if (isset($_REQUEST['product_state2']))
		{
			$product_state2=intval($_REQUEST['product_state2']);
			$sql_where.=" AND (product_state2 = {$product_state2})";
		}
		
		if (isset($_REQUEST['public_state']))
		{
			$public_state=intval($_REQUEST['public_state']);
			// force dont show any products with public_state == 0
			if (!exponent_permissions_check(array("adminisatrate", "configure"),$loc) && ($public_state == 0))
			{
				$public_state=1;
			}
			$sql_where.=" AND (public_state = {$public_state})";
		}
		
		if (isset($_REQUEST['hot_product']))
		{
			$sql_where.=" AND (hot_product = 1)";
		}
		
		if (isset($_REQUEST['on_sale']))
		{
			$sql_where.=" AND (on_sale = 1)";
		}
		
		if (isset($_REQUEST['best_seller']))
		{
			$sql_where.=" AND (best_seller = 1)";
		}
		
		// sort
		$sort="";
		if (isset($_REQUEST['sortName']))
		{
			$sort=$_REQUEST['sortName'];
			// for some field has been faked, we should turn them into the right field name
			if ($sort == "best_seller_checkbox")
				$sort="best_seller";
			else if ($sort == "on_sale_checkbox")
				$sort = "on_sale";
			else if ($sort == "hot_product_checkbox")
				$sort = "hot_product";
			else if ($sort == "public_state_checkbox")
				$sort = "public_state";
			else if ($sort == "cat_name")
				$sort = "cat_id";
			else if ($sort == "brand_name")
				$sort = "brand_id";
			else if ($sort == "product_state_text")
				$sort = "product_state";
			else if ($sort == "product_state2_text")
				$sort = "product_state2";
				
			$sortOrder="ASC";
			if (isset($_REQUEST['sortOrder']) && ($_REQUEST['sortOrder'] != "asc"))
				$sortOrder = "DESC";
			$sort = " ORDER BY {$sort} {$sortOrder}";
		}
				
		$count=$db->countObjects("productsmodule_products", "location_data = '".serialize($loc)."' {$sql_where}");
		
		// pagination
		$page=1;
		if (isset($_REQUEST['page']))
			$page=intval($_REQUEST['page']);
		$pageSize=10;
		if (isset($_REQUEST['pageSize']))
			$pageSize=intval($_REQUEST['pageSize']);
		$start=($page-1)*$pageSize;
		
		$config=$db->selectObject("productsmodule_config", "location_data = '".serialize($loc)."'");
		
		$data=$db->selectObjects("productsmodule_products", "location_data = '".serialize($loc)."' {$sql_where} {$sort} LIMIT {$start}, {$pageSize}");
		$price_max=0; $price_min=0;
		$data=productsmodule::__ProductProcess($data,$config, $price_max, $price_min);
		
		// for more compatible with jquery easy ui, should unset spec and description
		foreach ($data as $k=>$v)
		{
			$v->spec="";
			$v->description="";
			$v->search_spec="";
			$v->search_description="";
		}
		
		// prepare for return json
		$ret=null;
		$ret->total=$count;
		$ret->rows=$data;
		echo json_encode($ret);
	}
	die();
?>